package com.hospital.diagnose.impl;

import com.alibaba.fastjson.JSON;
import com.hospital.diagnose.dao.PatientMapper;
import com.hospital.diagnose.dao.TransferMapper;
import com.hospital.diagnose.entity.Patient;
import com.hospital.diagnose.entity.PatientExample;
import com.hospital.diagnose.entity.Transfer;
import com.hospital.diagnose.entity.TransferExample;
import com.hospital.diagnose.service.TransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @package_name com.hospital.diagnose.impl
 * @date 2019/1/10 15:20
 * @description
 */
@Service
public class TransferServiceImpl implements TransferService {

    @Autowired
    PatientMapper patientMapper;
    @Autowired
    TransferMapper transferMapper;

    /**
     * 查询所有转院记录
     */
    @Override
    public String getAllInformation() {
        TransferExample transferExample = new TransferExample();

        TransferExample.Criteria criteria = transferExample.createCriteria();

        transferExample.setOrderByClause("id desc");

        criteria.getAllCriteria();

        List<Transfer> list = transferMapper.selectByExample(transferExample);

        List<Map<String, String>> resultMap = new ArrayList<>();
        //日期格式转换
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, String> map;
        for (Transfer transfer : list) {
            Patient patient = patientMapper.selectByPrimaryKey(transfer.getTransPatId());
            String name = patient.getPatName();
            Integer s = patient.getPatSex();
            String sex;
            if (s == 1) {
                sex = "男";
            } else {
                sex = "女";
            }
            String age = String.valueOf(patient.getPatAge());
            String phone = patient.getPatContactInfo();

            map = new HashMap<>();
            map.put("id", transfer.getId().toString());
            map.put("patName", name);
            map.put("patSex", sex);
            map.put("patAge", age);
            map.put("patContactInfo", phone);
            map.put("transTowhere", transfer.getTransTowhere());
            map.put("transTime", sdf.format(transfer.getTransTime()).toString());
            map.put("transPhysician", transfer.getTransPhysician());
            map.put("transRemark", transfer.getTransRemark());
            resultMap.add(map);
        }
        return JSON.toJSONString(resultMap);
    }

    /**
     * 通过病人名字查找信息
     */
    @Override
    public String selectByPatName(String patName) {
        List<Map<String, String>> resultMap = new ArrayList<>();

        PatientExample patientExample = new PatientExample();
        PatientExample.Criteria criteria = patientExample.createCriteria();
        criteria.andPatNameEqualTo(patName);
        List<Patient> patientList = patientMapper.selectByExample(patientExample);
//        System.out.println(patientList);
        for (Patient patient : patientList) {
            Integer patId = patient.getId();
            TransferExample transferExample = new TransferExample();
            TransferExample.Criteria criteria1 = transferExample.createCriteria();
            criteria1.andTransPatIdEqualTo(patId);
            List<Transfer> transferList = transferMapper.selectByExample(transferExample);

            //日期格式转换
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Map<String, String> map;
            for (Transfer transfer : transferList) {
                Patient patient1 = patientMapper.selectByPrimaryKey(transfer.getTransPatId());
                String name = patient1.getPatName();
                Integer s = patient1.getPatSex();
                String sex;
                if (s == 1) {
                    sex = "男";
                } else {
                    sex = "女";
                }
                String age = String.valueOf(patient1.getPatAge());
                String phone = patient1.getPatContactInfo();

                map = new HashMap<>();
                map.put("id", transfer.getId().toString());
                map.put("patName", name);
                map.put("patSex", sex);
                map.put("patAge", age);
                map.put("patContactInfo", phone);
                map.put("transTowhere", transfer.getTransTowhere());
                map.put("transTime", sdf.format(transfer.getTransTime()).toString());
                map.put("transPhysician", transfer.getTransPhysician());
                map.put("transRemark", transfer.getTransRemark());
                resultMap.add(map);
            }
        }
        return JSON.toJSONString(resultMap);
    }

    /**
     * 通过转院时间查找信息
     *
     * @param transTime
     * @return
     */
    @Override
    public String selectByTransTime(Date transTime) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        Date time=new Date();
//        time=sdf.parse(transTime);
            TransferExample transferExample = new TransferExample();
            TransferExample.Criteria criteria = transferExample.createCriteria();
            criteria.andTransTimeEqualTo(transTime);
            List<Transfer> transferList = transferMapper.selectByExample(transferExample);

            List<Map<String, String>> result = new ArrayList<>();
            Map<String, String> map;
            for (Transfer transfer : transferList) {
                Integer patId = transfer.getTransPatId();
                Patient patient = patientMapper.selectByPrimaryKey(patId);
                map = new HashMap<>();
                Integer s = patient.getPatSex();
                String sex;
                if (s == 1) {
                    sex = "男";
                } else {
                    sex = "女";
                }
                map.put("id", transfer.getId().toString());
                map.put("patName", patient.getPatName());
                map.put("patSex", sex);
                map.put("patAge", patient.getPatAge().toString());
                map.put("patContactInfo", patient.getPatContactInfo());
                map.put("transTowhere", transfer.getTransTowhere());
                map.put("transTime", sdf.format(transfer.getTransTime()).toString());
                map.put("transPhysician", transfer.getTransPhysician());
                map.put("transRemark", transfer.getTransRemark());
                result.add(map);
            }
            return JSON.toJSONString(result);
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString("发生未知错误");
        }
    }

    //通过id删除一条信息
    @Override
    public String delOneInfoById(Integer id) {
        TransferExample transferExample = new TransferExample();
        TransferExample.Criteria criteria = transferExample.createCriteria();
        criteria.andIdEqualTo(id);
//        criteria.getAllCriteria();
        Integer transferList = transferMapper.deleteByExample(transferExample);

        TransferExample transferExample1 = new TransferExample();
        TransferExample.Criteria criteria1 = transferExample1.createCriteria();
        criteria1.getAllCriteria();
        if (transferList == 1) {
//            List<Transfer> transferList1=transferMapper.selectByExample(transferExample1);
            return getAllInformation();
        } else {
            Map<String, String> map = new HashMap<>();
            map.put("info", "删除失败");
            return JSON.toJSONString(map);
        }
    }
    //添加一条新数据

    @Override
    public String addOneInfo(Transfer transfer) {
        transferMapper.insert(transfer);
        return getAllInformation();
    }

    @Override
    public Integer findPatIdByName(String name) {
        PatientExample patientExample = new PatientExample();
        PatientExample.Criteria criteria = patientExample.createCriteria();
        criteria.andPatNameEqualTo(name);
        List<Patient> patients = patientMapper.selectByExample(patientExample);
        return patients.get(0).getId();
    }


    @Override
    public Transfer findTransferInfoById(Integer id) {
        return transferMapper.selectByPrimaryKey(id);
    }

    @Override
    public String updateInfo(Transfer transfer) {
        TransferExample transferExample = new TransferExample();
        TransferExample.Criteria criteria = transferExample.createCriteria();
        criteria.andIdEqualTo(transfer.getId());
        transferMapper.updateByExampleSelective(transfer,transferExample);
        return getAllInformation();
    }
}
